﻿namespace EFS.Common.UI.Controls
{
	/// <summary>
	/// Modal dialog host
	/// </summary>
	public partial class ModalDialogHost : EFS.Common.UI.Controls.ChildWindow
	{
		#region Composition

		/// <summary>
		/// Result
		/// </summary>
		public enum ClosedReason { NotSet, Ok, Cancel }

		// closedReason
		private EFS.Common.UI.Controls.ModalDialogHost.ClosedReason closedReason = EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.NotSet;

		// actual size
		System.Double actualWidth = 0;
		System.Double actualHeight = 0;

		#endregion

		#region Delegates

		/// <summary>
		/// Ok Click
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <param name="dialogResult"></param>
		public delegate void ButtonOKClick(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult);
		public ButtonOKClick OnButtonOKClick;

		/// <summary>
		/// Cancel Click
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <param name="dialogResult"></param>
		public delegate void ButtonCancelClick(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult);
		public ButtonCancelClick OnButtonCancelClick;

		/// <summary>
		/// Closing
		/// </summary>
		/// <param name="modalDialogHost"></param>
		/// <returns></returns>
		public delegate System.Boolean HostClosing(EFS.Common.UI.Controls.ModalDialogHost modalDialogHost);
		public HostClosing OnHostClosing;

		/// <summary>
		/// Closed delegate
		/// </summary>
		public delegate void HostClosed(EFS.Common.UI.Controls.ModalDialogHost host);
		private HostClosed OnHostClosed;

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		public ModalDialogHost()
		{
			InitializeComponent();

			// register close event
			this.Closed += ModalDialogHost_Closed;
		}

		#endregion

		#region Properties

		/// Property HostedContent: Get, Set
		public System.Windows.Controls.UserControl HostedContent
		{
			get
			{
				return (System.Windows.Controls.UserControl)contentHost.Content;
			}
			set
			{
				contentHost.Content = value;
			}
		}

		/// Property Title: Get, Set
		public new System.String Title
		{
			set
			{
				base.Title = value;
			}
			get
			{
				return null != base.Title ? base.Title.ToString() : "";
			}
		}

		/// Property Result: Get, Set
		public EFS.Common.UI.Controls.ModalDialogHost.ClosedReason DialogClosedReason
		{
			get
			{
				return closedReason;
			}
		}

		/// Property ButtonOKVisible: Get, Set
		public System.Boolean ButtonOKVisible
		{
			get
			{
				return System.Windows.Visibility.Visible == buttonOK.Visibility;
			}
			set
			{
				buttonOK.Visibility = value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			}
		}

		/// Property ButtonCancelVisible: Get, Set
		public System.Boolean ButtonCancelVisible
		{
			get
			{
				return System.Windows.Visibility.Visible == buttonCancel.Visibility;
			}
			set
			{
				buttonCancel.Visibility = value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			}
		}

		#endregion

		#region Visibility

		/// <summary>
		/// Show
		/// </summary>
		/// <param name="closedCallback"></param>
		public void Show(EFS.Common.UI.Controls.ModalDialogHost.HostClosed onHostClosed)
		{
			// test callback
			if (null != onHostClosed)
			{
				OnHostClosed = onHostClosed;
			}

			// show
			base.Show();
		}

		/// Close
		public void Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason closedReason)
		{
			this.closedReason = closedReason;
			this.DialogResult = closedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok;
		}

		#endregion

		#region Layout

		/// <summary>
		/// Applly template
		/// </summary>
		public override void OnApplyTemplate()
		{
			// base
			base.OnApplyTemplate();

			// get root
			System.Windows.FrameworkElement contentRoot = this.GetTemplateChild("ContentRoot") as System.Windows.FrameworkElement;

			// get update events
			if (null != contentRoot)
			{
				contentRoot.LayoutUpdated += ContentRoot_LayoutUpdated;
			}
		}

		#endregion

		#region Events

		/// <summary>
		/// Payout update
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void ContentRoot_LayoutUpdated(object sender, System.EventArgs e)
		{
			// get root
			System.Windows.FrameworkElement contentRoot = this.GetTemplateChild("ContentRoot") as System.Windows.FrameworkElement;

			// get update events
			if (null != contentRoot)
			{
				// re-transform flag
				System.Boolean reTransform = false;

				// check height
				if (contentRoot.ActualHeight != actualHeight)
				{
					actualHeight = contentRoot.ActualHeight;
					reTransform = true;
				}

				// check height
				if (contentRoot.ActualWidth != actualWidth)
				{
					actualWidth = contentRoot.ActualWidth;
					reTransform = true;
				}

				// check
				if (reTransform)
				{
					// tg
					System.Windows.Media.TransformGroup transformGroup = contentRoot.RenderTransform as System.Windows.Media.TransformGroup;
					if (null != transformGroup)
					{
						// get transforms
						foreach (System.Windows.Media.Transform transform in transformGroup.Children)
						{
							// filter translates
							if (transform is System.Windows.Media.TranslateTransform)
							{
								// set 0;0
								System.Windows.Media.TranslateTransform translateTransform = transform as System.Windows.Media.TranslateTransform;
								translateTransform.X = 0;
								translateTransform.Y = 0;
							}
						}
					}
				}
			}
		}


		/// <summary>
		/// Closed event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void ModalDialogHost_Closed(object sender, System.EventArgs e)
		{
			// remove
			this.Closed -= ModalDialogHost_Closed;

			// closed delegate
			if (null != OnHostClosed)
			{
				// call
				OnHostClosed(this);
			}
		}

		/// <summary>
		/// Closing
		/// </summary>
		/// <param name="e"></param>
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			// check delegate
			if (null != OnHostClosing)
			{
				// delegate can prevent closing
				if (!OnHostClosing(this))
				{
					e.Cancel = true;
				}
			}

			// call base
			base.OnClosing(e);
		}

		/// Button OK
		private void buttonOK_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (null != this.OnButtonOKClick)
			{
				this.OnButtonOKClick(sender, e, true);
			}
			else
			{
				Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
		}

		/// Button Cancel
		private void buttonCancel_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (null != this.OnButtonCancelClick)
			{
				this.OnButtonCancelClick(sender, e, false);
			}
			else
			{
				Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Cancel);
			}
		}

		#endregion
	}

}